package com.everyline.baselibrary.utils

import android.annotation.SuppressLint
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 *creatd by 天天挂线
 *email:746608431@qq.com
 *on 2020/7/9
 *描述:TODO
 */
object DateTimeUtils {
    val DATE_PATTERN = "yyyy-MM-dd"
    var DATE_PATTERN_1 = "yyyy-MM-dd HH:mm:ss"
    var DATE_PATTERN_2 = "yyyy-MM-dd HH:mm"
    var DATE_PATTERN_3 = "MM月dd日"
    var DATE_PATTERN_4 = "MM月dd日 HH:mm"
    var DATE_PATTERN_5 = "yyyy年MM月dd日 HH:mm"
    var DATE_PATTERN_6 = "yyyy年MM月dd日 HH:mm:ss"
    var DATE_PATTERN_7 = "HH:mm"


    const val TODAY = "今天"
    const val YESTERDAY = "昨天"
    const val TOMORROW = "明天"
    const val BEFORE_YESTERDAY = "前天"
    const val AFTER_TOMORROW = "后天"

    /**
     * 获取对应时间戳转换的今天、明天。。。。的日期 * @param time * @return
     */
    fun getToday(time: String): String? {
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.time = predate
        val cal = Calendar.getInstance()
        var date: Date? = null
        try {
            date = SimpleDateFormat("yyyy-MM-dd").parse(time)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        cal.time = date
        if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
            val diffDay = (cal[Calendar.DAY_OF_YEAR]
                    - pre[Calendar.DAY_OF_YEAR])
            return showDateDetail(diffDay, time)
        }
        return time
    }

    /**
     * 获取对应时间戳转换的今天、明天。。。。的日期 * @param time * @return
     */
    fun getTodayLong(time: String): String? {
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.time = predate
        val cal = Calendar.getInstance()
        var date: Date? = null
        try {
            date = SimpleDateFormat("yyyy-MM-dd").parse(time)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        cal.time = date
        if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
            val diffDay = (cal[Calendar.DAY_OF_YEAR]
                    - pre[Calendar.DAY_OF_YEAR])
            return showDateDetail(diffDay, time)
        }
        return null
    }

    /**
     * 将日期差显示为今天、明天或者星期 * @param diffDay * @param time * @return
     */
    private fun showDateDetail(diffDay: Int, time: String): String? {
        return when (diffDay) {
            0 -> TODAY
            1 -> TOMORROW
            2 -> AFTER_TOMORROW
            -1 -> YESTERDAY
            -2 -> BEFORE_YESTERDAY
            else -> time
        }
    }


    /**
     * 获取现在时刻
     */
    val nowDate: Date
        get() = Date(Date().time)

    /**
     * 获取现在时刻
     */
    val nowTimeYMD: Date
        get() = formatDate(DATE_PATTERN, nowDate)

    /**
     * Date to Strin
     */
    @SuppressLint("SimpleDateFormat")
    fun formatDate(date: Date?, formatStyle: String): String {
        return if (date != null) {
            val sdf = SimpleDateFormat(formatStyle)
            sdf.format(date)
        } else {
            ""
        }

    }

    /**
     * Date to Strin
     */
    @SuppressLint("SimpleDateFormat")
    fun formatDate(date: Long, formatStyle: String): String {
        val sdf = SimpleDateFormat(formatStyle)
        return sdf.format(Date(date))

    }

    fun formatDate(formatStyle: String, formatStr: String): Date {
        val format = SimpleDateFormat(formatStyle, Locale.CHINA)
        return try {
            val date = Date()
            date.time = format.parse(formatStr).time
            date
        } catch (e: Exception) {
            println(e.message)
            nowTimeYMD
        }

    }

    /**
     * Date to Date
     */
    @SuppressLint("SimpleDateFormat")
    fun formatDate(formatStyle: String, date: Date?): Date {
        if (date != null) {
            val sdf = SimpleDateFormat(formatStyle)
            val formatDate = sdf.format(date)
            try {
                return sdf.parse(formatDate)
            } catch (e: ParseException) {
                e.printStackTrace()
                return Date()
            }

        } else {
            return Date()
        }
    }

    fun getTimeYMD(date: Date): String? { //可根据需要自行截取数据显示
        val format = SimpleDateFormat(DATE_PATTERN)
        return format.format(date)
    }

    /**
     * 将时间戳转换为时间
     */
    fun stampToDate(s: String): Date {
        val lt = s.toLong()
        return Date(lt)
    }

    /**
     * 获得指定时间的日期
     */
    fun getCustomTime(dateStr: String): Date {
        return formatDate(DATE_PATTERN, dateStr)
    }


    //获取明天
    fun getTomorrow(date: Date?): Date {
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar.add(Calendar.DAY_OF_YEAR, 1)
        return calendar.time
    }

    //获取昨天
    fun getYesterday(date: Date?): Date {
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar.add(Calendar.DAY_OF_YEAR, -1)
        return calendar.time
    }

    //获取明天
    fun getTomorrow(): String {
        val format = SimpleDateFormat("yyyy-MM-dd")
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_YEAR, 1)
        return format.format(calendar.time)
    }

    //获取昨天
    fun getYesterday(): String {
        val format = SimpleDateFormat("yyyy-MM-dd")
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_YEAR, -1)
        return format.format(calendar.time)
    }

    //获取未来7天
    fun getTomorrow7(): String {
        val format = SimpleDateFormat("yyyy-MM-dd")
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_YEAR, 7)
        return format.format(calendar.time)
    }
    //获取未来15天
    fun getTomorrow16(): String {
        val format = SimpleDateFormat("yyyy-MM-dd")
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_YEAR, 16)
        return format.format(calendar.time)
    }
    //判断2个时间大小

    //判断2个时间大小
    fun compare_date(date1: String?, date2: String?): Int {
        val df: DateFormat = SimpleDateFormat("yyyy-MM-dd")
        try {
            val dt1 = df.parse(date1)
            val dt2 = df.parse(date2)
            return when {
                dt1.time > dt2.time -> {

                    1
                }
                dt1.time < dt2.time -> {

                    -1
                }
                else -> {

                    0
                }
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }
        return 0
    }


    /*
    *  判断当前时间是否在设置的dark mode时间段内
    *  @param date1: 开始时间（hh:mm）
    *  @param date2: 结束时间（hh:mm）
    */
    fun isBelongPeriodTime(date1: String, date2: String): Boolean {
        val calendar = Calendar.getInstance()
        val week = calendar.get(Calendar.DAY_OF_WEEK)
        val df = SimpleDateFormat("HH:mm")
        val currentTime = Date(System.currentTimeMillis())
        val startTimeDate: Date
        val endTimeDate: Date
        val date = Calendar.getInstance()
        val begin = Calendar.getInstance()
        val end = Calendar.getInstance()
        try {
            date.time = df.parse(df.format(currentTime))
            startTimeDate = df.parse(date1)
            endTimeDate = df.parse(date2)
            begin.time = startTimeDate
            end.time = endTimeDate
            if (endTimeDate.hours < startTimeDate.hours) {
                return (week == Calendar.MONDAY ||
                        week == Calendar.TUESDAY ||
                        week == Calendar.WEDNESDAY) && (date.after(begin) || date.before(end))
            } else if (endTimeDate.hours == startTimeDate.hours) {
                if (endTimeDate.minutes < startTimeDate.minutes) {
                    return (week == Calendar.MONDAY ||
                            week == Calendar.TUESDAY ||
                            week == Calendar.WEDNESDAY) && (date.after(begin) || date.before(end))
                }
            }
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        //这里是时间段的起止都在同一天的情况，只需要判断当前时间是否在这个时间段内即可
        return (week == Calendar.MONDAY ||
                week == Calendar.TUESDAY ||
                week == Calendar.WEDNESDAY) && date.after(begin) && date.before(end)
    }

    /*
   *  判断当前时间是否在设置的dark mode时间段内
   *  @param date1: 开始时间（hh:mm）
   *  @param date2: 结束时间（hh:mm）
   */
    fun isBelongPeriodTime2(date1: String, date2: String): Boolean {
        val calendar = Calendar.getInstance()
        val week = calendar.get(Calendar.DAY_OF_WEEK)
        val df = SimpleDateFormat("HH:mm")
        val currentTime = Date(System.currentTimeMillis())
        val startTimeDate: Date
        val endTimeDate: Date
        val date = Calendar.getInstance()
        val begin = Calendar.getInstance()
        val end = Calendar.getInstance()
        try {
            date.time = df.parse(df.format(currentTime))
            startTimeDate = df.parse(date1)
            endTimeDate = df.parse(date2)
            begin.time = startTimeDate
            end.time = endTimeDate

            return when (week) {
                Calendar.MONDAY -> {
                    date.after(begin)
                }
                Calendar.TUESDAY -> {
                    true
                }
                Calendar.WEDNESDAY -> {
                    date.before(end)
                }
                else -> {
                    false
                }
            }

        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 计算 date2 - date1 的天数
     *
     * @param date1 yyyy-MM-dd格式的String
     * @param date2 yyyy-MM-dd格式的String
     * @return
     */
    fun cutStrDate(date1: String?, date2: String?): Int {
        var result: Long = 0
        try {
            val m1 = SimpleDateFormat("yyyy-MM-dd").parse(date1).time
            val m2 = SimpleDateFormat("yyyy-MM-dd").parse(date2).time
            result = m2 - m1
        } catch (e: ParseException) {
        }
        return (result / 24 / 60 / 60 / 1000).toInt()
    }

    fun cutStrDate2(date1: String?, date2: String?): Int {
        var result: Long = 0
        try {
            val m1 = SimpleDateFormat("yyyy年MM月dd日").parse(date1).time
            val m2 = SimpleDateFormat("yyyy年MM月dd日").parse(date2).time
            result = m2 - m1
        } catch (e: ParseException) {
        }
        return (result / 24 / 60 / 60 / 1000).toInt()
    }

}